package 力扣;

import java.util.ArrayList;
import java.util.List;

/**
 给定一个 没有重复 数字的序列，返回其所有可能的全排列。
 示例:
 输入: [1,2,3]
 输出:
 [
 [1,2,3],
 [1,3,2],
 [2,1,3],
 [2,3,1],
 [3,1,2],
 [3,2,1]
 ]
 */

public class _46全排列 {

    public List<List<Integer>> permute(int[] nums) {
        //回溯法
        List<List<Integer>> ret = new ArrayList<List<Integer>>();
        List<Integer> permuteList= new ArrayList<Integer>();
        boolean[] visited = new boolean[nums.length];
        backtracking(visited,nums,permuteList,ret);
        return ret;
    }

    private void backtracking(boolean[] visited, int[] nums, List<Integer> permuteList, List<List<Integer>> ret) {
        if( nums.length == permuteList.size()){
            ret.add(new ArrayList<>(permuteList));
            return;
        }
        for (int i = 0; i < nums.length ; i++) {
            if( visited[i]) continue;
            permuteList.add(nums[i]);
            visited[i] = true;
            backtracking(visited,nums,permuteList,ret);
            visited[i] = false;
            permuteList.remove(permuteList.size()-1);
        }
    }


}
